package com.douqu.game.core.entity.world;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.AuctionConfig;
import com.douqu.game.core.e.E_AuctionStatus;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.LogUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: miracle.
 * Description:
 * Date: 2018/3/20 0020 下午 7:12
 * Huan Yu Copyright (c) 2017 All Rights Rese rved.
 */
public class WorldAuctionData extends BaseData {

    private Map<Integer, WorldAuctionBean> auctionMap = new ConcurrentHashMap<Integer, WorldAuctionBean>();

    private int count;

    /**
     * 对拍卖map进行排序
     * @return
     */
    public Map<Integer,WorldAuctionBean> sortAuctionMap(){
        if(auctionMap.isEmpty()){
            return new TreeMap<>();
        }
        Map<Integer,WorldAuctionBean> sortMap = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        sortMap.putAll(auctionMap);
        //根据key 排序完成返回
        return sortMap;

    }

    @Override
    public void init() {
        auctionMap =  new ConcurrentHashMap<Integer, WorldAuctionBean>();
        count = 1;
    }

    @Override
    public void checkInit() {
        if(auctionMap == null){
            auctionMap =  new ConcurrentHashMap<Integer, WorldAuctionBean>();
        }
    }

    @Override
    public void reset() {
        checkReset();
    }

    /**
     * 没隔几秒调用一次 检测所有的未结束的
     * 如果时间大于结束时间 结束拍卖
     */
    @Override
    public void checkReset() {

    }

    /**
     * 拍卖物品的红点
     */
    public boolean redAuction(){
        for(Map.Entry<Integer,WorldAuctionBean> entry:auctionMap.entrySet()){
            if(entry.getValue().getState() == E_AuctionStatus.INT_THE_AUCTION.getCode())
                return true;
        }
        return false;
    }


    @Override
    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeInt(count);
        buffer.writeShort(auctionMap.size());
        for(Map.Entry<Integer, WorldAuctionBean> entry : auctionMap.entrySet())
        {
            buffer.writeInt(entry.getKey());
            entry.getValue().writeTo(buffer);
        }

        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        count = buffer.readInt();
        int size = buffer.readShort();
        WorldAuctionBean worldAuctionBean = null;
        Integer key;
        for(int i = 0; i < size; i++){
            key = buffer.readInt();
            worldAuctionBean = new WorldAuctionBean();
            worldAuctionBean.loadFrom(buffer);
            auctionMap.put(key, worldAuctionBean);
        }

        buffer.readBoolean();
    }


    public void update(long currentTime)
    {
//        //遍历所有的拍卖是否结束
//        for (Map.Entry<Integer, WorldAuctionBean> entry : auctionMap) {
//            //2.如果没有结束
//            if (entry.getValue().getState() == 0) {
//                //判断是否到时间
//                //3.获取配置表中的keepTime
//                AuctionConfig auctionConfig = WorldAuctionData.getAuctionConfig(entry.getValue().getAuctionConfigId());
//                if (currentTime >= (entry.getValue().getStartTime() + auctionConfig.keepTime)) {
//                    //如果当前的时间大于  开始时间  + 持续时间  那么结束
//                    //4.判断是否有人出价
//                    if(Utils.isNullOrEmpty(entry.getValue().getCurrentPlayer()))
//                    if (entry.getValue().getCurrentPlayer() == null || entry.getValue().getCurrentPlayer().trim().equals("")) {
//                        ((BoonMsgChannel)SpringContext.getMessageChannel(BoonMsgChannel.class)).auctionSuccess(entry.getValue(), false);
//                    } else {
//                        ((BoonMsgChannel)SpringContext.getMessageChannel(BoonMsgChannel.class)).auctionSuccess(entry.getValue(), true);
//                    }
//                }
//            }
//        }
    }

    public void newAuction(int id, List<String> playerList,int source){
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        newAuction(ids, playerList,source);
    }
    /**
     * boss死亡后调用此方法实现增加新的拍卖
     * @param auctionConfigIds    配置表的id列表
     * @param playerList   用户list
     * @Param source 来源source
     * 多个拍卖 对应的是同一个用户list
     */
    public void newAuction(List<Integer> auctionConfigIds, List<String> playerList,int source){
        //1.读取配置文件找到要拍卖物品的list
        //遍历所有的数据
        if(playerList == null){
            playerList = new ArrayList<>();
        }
        AuctionConfig auctionConfig;
        WorldAuctionBean bean;
        for (Integer auctionConfigId:auctionConfigIds){
            auctionConfig = getAuctionConfig(auctionConfigId);
            if(auctionConfig == null){
                LogUtils.error("找不到id为" + auctionConfigId + "的拍卖行配置信息");
                return;
            }
            bean = new WorldAuctionBean();
            bean.setCurrentPrice(auctionConfig.price.value);
            bean.setCurrentPlayer("");
            bean.setAuctionConfigId(auctionConfig.id);
            bean.setState(E_AuctionStatus.INT_THE_AUCTION.getCode());
            bean.setStartTime(DataFactory.currentTime);
            bean.setPlayerList(playerList);
            bean.setAuctionList(new ArrayList<>());
            bean.setId(count++);
            bean.setSource(source);
            //2.初始化auctionBean
            //模拟加入一个用户
            auctionMap.put(bean.getId(), bean);
//        List<Player> players = new ArrayList<Player>();
//        for (String playerIndex:playerList){
//            //根据流水号获取player
//            Player player = GameServer.getInstance().getWorldManager().getPlayerController(playerIndex).getPlayer();
//            players.add(player);
//        }
        }


    }


    public WorldAuctionBean getRecord(int id)
    {
        return auctionMap.get(id);
    }

    /**
     * 根据配置表的id获取正确的拍卖行配置
     * @param auctionConfigId
     * @return
     */
    public static AuctionConfig getAuctionConfig(int auctionConfigId){
        return DataFactory.getInstance().getGameObject(DataFactory.WORLD_AUCTION_KEY, auctionConfigId);
    }

    public Map<Integer, WorldAuctionBean> getAuctionMap() {
//        auctionMap = new ConcurrentHashMap<>();
//        List<String> playerList = new ArrayList<>();
//        playerList.add("8100219OA2QHG9");
//        playerList.add("8100217YJTH7Q4");
//        List <Integer> auctionConfigIds = new ArrayList<>();
//        auctionConfigIds.add(101);
//        auctionConfigIds.add(102);
//        auctionConfigIds.add(111);
//        auctionConfigIds.add(112);
//        auctionConfigIds.add(113);
//        newAuction(auctionConfigIds, playerList, E_AuctionSource.AUCTION_SOURCE_BOSS.getCode());
        return auctionMap;
    }

    public void setAuctionMap(Map<Integer, WorldAuctionBean> auctions) {
        this.auctionMap = auctionMap;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
